﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using System.Data;
namespace Services
{

    public partial class Class_FeeTimeService
    {
       readonly IClass_FeeTimeEntity entity;
       readonly IStudent_FeeEntity student_feeemtity;
       readonly IStudentEntity studententity;
       readonly IClassEntity Classeentity;
       readonly ICourseEntity Courseentity;
       public Class_FeeTimeService()
       {
           this.entity = new Class_FeeTimeEntity();
           student_feeemtity = new Student_FeeEntity();
           studententity = new StudentEntity();
           Classeentity = new ClassEntity();
           Courseentity = new CourseEntity();

       }

       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model)
       {
           try
           {
               var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
               if (id == 0)
               {
                   return entity.Save((CoreData.Class_FeeTime)_model, Table.Class_FeeTime.ToString());
               }
               else
               {
                   return entity.Update((CoreData.Class_FeeTime)_model, Table.Class_FeeTime.ToString());
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.Class_FeeTime GetByID(long _id)
       {
           try
           {
               return entity.GetById(_id, Table.Class_FeeTime.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Class_FeeTime> GetList()
       {
           try
           {
               return entity.GetAll(Table.Class_FeeTime.ToString()).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Class_FeeTime> GetListByLINQ(Func<CoreData.Class_FeeTime, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.Class_FeeTime.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.Class_FeeTime GetOneByLINQ(Func<CoreData.Class_FeeTime, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.Class_FeeTime.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Class_FeeTime> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetBySearchString(_searchstring, Table.Class_FeeTime.ToString()).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.Class_FeeTime)_model, Table.Class_FeeTime.ToString());
               return true;
           }
           catch { return false; }
       }


       #endregion



        #region Other Method
        /// <summary>
        /// Get by type
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
       public CoreData.Class_FeeTime GetByType(int type)
       {
           try
           {
               return entity.GetBySearchString(" Type="+type,Table.Class_FeeTime.ToString()).FirstOrDefault();
           }
           catch { return null; }
       }

        /// <summary>
        /// Lấy kỳ hạn đóng dựa vào ngày đóng
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
       public string GetFeeTimeString(DateTime time, int type, long Student_FeeID)
       {
           try
           {

               var studentid  = student_feeemtity.GetById(Student_FeeID, Table.Student_Fee.ToString()).StudentID;
               var data = entity.GetByCusTomSQL(String.Format(SQLCommand.GetClassFeeTimeBYStudentANDType, studentid, type)).FirstOrDefault();
             
               if(type==1)
               {
                   
                   return "Từ " + data.StartDate1.ToShortDateString() + " đến " + data.EndDate1.ToShortDateString();
               }
               if (type == 2)
               {
                   if (time >= data.StartDate1 && time <= data.EndDate1)
                   {
                       return "Lần 1: Từ " + data.StartDate1.ToShortDateString() + " đến " + data.EndDate1.ToShortDateString();
                   }
                   else
                   {
                       return "Lần 2: Từ " + data.StartDate2.ToShortDateString() + " đến " + data.EndDate2.ToShortDateString();
                   }
               }
               if (type == 3)
               {
                   if (time >= data.StartDate1 && time <= data.EndDate1)
                   {
                       return "Lần 1: Từ " + data.StartDate1.ToShortDateString() + " đến " + data.EndDate1.ToShortDateString();
                   }
                   else if (time >= data.StartDate2 && time <= data.EndDate2)
                   {
                       return "Lần 2: Từ " + data.StartDate2.ToShortDateString() + " đến " + data.EndDate2.ToShortDateString();
                   }
                   else {
                       return "Lần 3: Từ " + data.StartDate3.ToShortDateString() + " đến " + data.EndDate3.ToShortDateString();
                   }
               }
               if (type == 4)
               {
                   if (time >= data.StartDate1 && time <= data.EndDate1)
                   {
                       return "Lần 1: Từ " + data.StartDate1.ToShortDateString() + " đến " + data.EndDate1.ToShortDateString();
                   }
                   else if (time >= data.StartDate2 && time <= data.EndDate2)
                   {
                       return "Lần 2: Từ " + data.StartDate2.ToShortDateString() + " đến " + data.EndDate2.ToShortDateString();
                   }
                   else if (time >= data.StartDate3 && time <= data.EndDate3)
                   {
                       return "Lần 3: Từ " + data.StartDate3.ToShortDateString() + " đến " + data.EndDate3.ToShortDateString();
                   }
                   else
                   {
                       return "Lần 4: Từ " + data.StartDate4.ToShortDateString() + " đến " + data.EndDate4.ToShortDateString();
                   }
               }
               if (type == 6)
               {
                   if (time >= data.StartDate1 && time <= data.EndDate1)
                   {
                       return "Lần 1: Từ " + data.StartDate1.ToShortDateString() + " đến " + data.EndDate1.ToShortDateString();
                   }
                   else if (time >= data.StartDate2 && time <= data.EndDate2)
                   {
                       return "Lần 2: Từ " + data.StartDate2.ToShortDateString() + " đến " + data.EndDate2.ToShortDateString();
                   }
                   else if (time >= data.StartDate3 && time <= data.EndDate3)
                   {
                       return "Lần 3: Từ " + data.StartDate3.ToShortDateString() + " đến " + data.EndDate3.ToShortDateString();
                   }
                   else if (time >= data.StartDate4 && time <= data.EndDate4)
                   {
                       return "Lần 4: Từ " + data.StartDate4.ToShortDateString() + " đến " + data.EndDate4.ToShortDateString();
                   }
                   else if (time >= data.StartDate5 && time <= data.EndDate5)
                   {
                       return "Lần 5: Từ " + data.StartDate5.ToShortDateString() + " đến " + data.EndDate5.ToShortDateString();
                   }
                   else
                   {
                       return "Lần 6: Từ " + data.StartDate6.ToShortDateString() + " đến " + data.StartDate6.ToShortDateString();
                   }
               }
               return "Quá hạn";
           }

           catch { return null; }

       }

        /// <summary>
        /// Lấy thời hạn đóng bằng mã học viên và loại
        /// </summary>
        /// <param name="studentid"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public Class_FeeTime GetFeeTimeByStudentIDAndType(long studentid, int type)
       {
           try {
               var data = entity.GetByCusTomSQL(String.Format(SQLCommand.GetClassFeeTimeBYStudentANDType, studentid, type)).FirstOrDefault();
               return data;
           }
           catch { return null; }
        }

        /// <summary>
        /// Lất thời hạn đóng bằng mã học viên và loại
        /// </summary>
        /// <param name="studentid"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public DataTable GetFeeTimeByStudentIDAndTypeReturnDatatable(long studentid, int type)
        {
            try
            {
                var data = entity.GetByCusTomSQLReturnDatatable(String.Format(SQLCommand.GetClassFeeTimeBYStudentANDType, studentid, type));
                return data;
            }
            catch { return null; }
        }



        public Class_FeeTime GetFeeTimeByClassIDAndType(long classId, int type)
        {
            try {
                return entity.GetBySearchString(" ClassID=" + classId + " and  Type=" + type, Table.Class_FeeTime.ToString()).FirstOrDefault();
            }
            catch { return null; }
        }

      
        #endregion 
    
    }
         
}
